package com.awspaas.user.apps.shhtaerospaceindustrial.guaranteecar;

import java.util.List;
import java.util.Map;

import com.actionsoft.bpms.bpmn.engine.core.delegate.ProcessExecutionContext;
import com.actionsoft.bpms.bpmn.engine.listener.ExecuteListener;
import com.actionsoft.bpms.commons.database.ColumnMapRowMapper;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.awspaas.user.apps.shhtaerospaceindustrial.healthmeaasge.weComSendAnnouncementUtil;
import com.awspaas.user.apps.shhtaerospaceindustrial.util.CoreUtil;

public class sendApplyCarPeopleMessage extends ExecuteListener{
	
	public String getDescription() {
		return "调度已派单，给申请人、驾驶员发企业微信消息。";
	}

	@Override
	public void execute(ProcessExecutionContext param) throws Exception {
		boolean flag_ok = SDK.getTaskAPI().isChoiceActionMenu(param.getTaskInstance(), "派单");
		if(flag_ok) {
			//获取流程实例ID
			String processInstId = param.getProcessInstance().getId();
			//根据流程实例ID获取任务表内数据
			List<Map<String , Object>> headlist = DBSql.query("SELECT * FROM BO_EU_GUARANTEECAR  WHERE BINDID = ? ", 
					new ColumnMapRowMapper(), new Object[] {processInstId});
			Map<String , Object>  jcbd = headlist.get(0);
			String TARGETMAN = CoreUtil.objToStr(jcbd.get("TARGETMAN"));//申请人
			String TARGETMANPHONE = CoreUtil.objToStr(jcbd.get("TARGETMANPHONE"));//联系电话
			String VEHICLEROUTE = CoreUtil.objToStr(jcbd.get("VEHICLEROUTE"));//用车路线
			String CARBEGINTIME = CoreUtil.objToStr(jcbd.get("CARBEGINTIME"));//用车开始时间
			String CARENDTIME = CoreUtil.objToStr(jcbd.get("CARENDTIME"));//用车结束时间
			String DRIVER = CoreUtil.objToStr(jcbd.get("DRIVER"));//派出驾驶员
			String JSYDH = CoreUtil.objToStr(jcbd.get("JSYDH"));//驾驶员电话
			String LICENSENUM = CoreUtil.objToStr(jcbd.get("LICENSENUM"));//派出车辆牌号
			String WORKERID = CoreUtil.objToStr(jcbd.get("WORKERID"));//申请人账号
			String JSYZH = CoreUtil.objToStr(jcbd.get("JSYZH"));//驾驶员账号
			String TARGETUNIT = CoreUtil.objToStr(jcbd.get("TARGETUNIT"));//申请单位
			//锁定车辆
			if(!"".equals(LICENSENUM)) {
				String updatechepaiSql = "update BO_EU_BZYC_CARDDOCINFO set INMISSION='是' where VEHICLECODE ='" + LICENSENUM + "'";
				DBSql.update(updatechepaiSql);
			}
			//给申请人发企业微信消息
			if( !"".equals(WORKERID)  && !"".equals(TARGETUNIT)) {
				String text="【业务保障用车】"+TARGETMAN+"您好！调度已根据用车时间："+CARBEGINTIME+"--"+CARENDTIME+",用车路线："
						+ VEHICLEROUTE+"为您派单，车牌号："+LICENSENUM+",驾驶员："+DRIVER+"，联系电话："
						+JSYDH+",如有问题请及时联系驾驶员，谢谢！";
				sendMessage(text,WORKERID,TARGETUNIT);
			}
			//给驾驶员发企业微信消息
			if( !"".equals(JSYZH)  && !"".equals(TARGETUNIT)) {
				String text="【业务保障用车】"+DRIVER+"您好！调度已根据用车时间："+CARBEGINTIME+"--"+CARENDTIME+",用车路线："
						+ VEHICLEROUTE+"为您派单，车牌号："+LICENSENUM+",申请人："+TARGETMAN+"，联系电话："
						+TARGETMANPHONE+",如有问题请及时联系申请人，谢谢！";
				sendMessage(text,JSYZH,TARGETUNIT);
			}
		}
		
	}
	public void sendMessage(String str,String peoplezh,String unit) {
		String sqluserId ="select EXT5  from ORGUSER  where USERID='"+peoplezh+"'";
        String weixinid =CoreUtil.objToStr(DBSql.getString(sqluserId,"EXT5"));
        String sql = "select t.*  from BO_EU_SHSY_NOTICE_UNITLIST t  where t.unit ='"+unit+"'";
        List<Map<String, Object>> jbData = DBSql.query(sql, new ColumnMapRowMapper(), new Object[] {});
        if (jbData.size() > 0 && !"".equals(weixinid)) {
        	String agentid = CoreUtil.objToStr(jbData.get(0).get("AGENTID"));//应用id
			String corpid = CoreUtil.objToStr(jbData.get(0).get("CORPID"));// 企业微信单位id
			String agentsecretid = CoreUtil.objToStr(jbData.get(0).get("AGENTSECRETID"));//应用secretid
			String acessToken = weComSendAnnouncementUtil.getAccessToken(corpid,agentsecretid);
			weComSendAnnouncementUtil.sendWeChatMessage(weixinid,"","text","",str,0,agentid,acessToken);
        }
	}

}
